import urllib2
import base64
import optparse
import json

class prosperclient:
   username = None
   password = None
   server = 'https://api.prosper.com'

   # Entire set of listings
   listdict = {}

   def __init__(self, username = None, password = None, restoreFile = None):
      if restoreFile == None:
         if username == None:
            print 'user:',
            username = raw_input()
         else: print 'user: %s' % username
         self.username = username
         if password == None:
            import getpass
            import sys
            password = getpass.getpass('pw: ', sys.stderr)
         self.password = password

      self.initListingDB(restoreFile)

   def initListingDB(self, restoreFile):
      import cPickle
      if restoreFile == None:
         print 'Reading listing data from %s...' % self.server,
         # On startup, retrieve ALL listings
         self.listdict = self._retreiveAllListings()
         print 'Complete.'

         # Pickle it
         print 'Saving listing data to %s...' % 'prosper.pickle',
         fd = open('prosper.pickle', 'wb')
         cPickle.dump(self.listdict, fd, protocol = cPickle.HIGHEST_PROTOCOL)
         fd.close()
         print 'Complete.'
      else:
         print 'Reading listing data from %s...' % restoreFile,
         fd = open(restoreFile, 'rb')
         self.listdict = cPickle.load(fd)
         fd.close()
         print 'Complete.'

      # Write a CSV for browsing
      import csv
      with open('prosper.csv', 'wb') as fd:
         w = csv.DictWriter(fd, self.listdict[0].keys())
         w.writeheader()
         for loan in self.listdict:
            w.writerow(loan)


      # Parse initial listing data
      #loans = self.listdict
      #for loan in loans:
      #   for key, val in loan.items():
      #      if not key.isupper():
      #         print key,val


   def test(self):
      request = urllib2.Request(self.server + '/api/Listings?$top=3')
      base64string = base64.encodestring('%s:%s' %
         (self.username, self.password)).replace('\n', '')
      request.add_header('Authorization', 'Basic %s' % base64string)
      #request.add_header('Accept', 'text/xml')

      result = urllib2.urlopen(request)
      print json.loads(result.read())

   def _retreiveAllListings(self):
      request = urllib2.Request(self.server + '/api/Listings')
      base64string = base64.encodestring('%s:%s' %
         (self.username, self.password)).replace('\n', '')
      request.add_header('Authorization', 'Basic %s' % base64string)
      result = urllib2.urlopen(request)
      return json.loads(result.read())

   def printListings(self, listdict = None):
      if listdict == None:
         listdict = self.listdict

      ALL = '''AmountDelinquent, AmountParticipation, AmountRemaining, BankcardUtilization, BorrowerAPR, BorrowerCity, BorrowerListingDescription, BorrowerMetropolitanArea, BorrowerRate, BorrowerState, ChannelCode, CreditLinesLast7Years, CreditPullDate, CurrentCreditLines, CurrentDelinquencies, DTIwProsperLoan, DelinquenciesLast7Years, DelinquenciesOver30Days, DelinquenciesOver60Days, DelinquenciesOver90Days, EffectiveYield, EmploymentStatusDescription, EstimatedLossRate, EstimatedReturn, FICOScore, FirstRecordedCreditLine, FundingThreshold, GroupIndicator, GroupName, IncomeRange, IncomeRangeDescription, IncomeVerifiable, InquiriesLast6Months, InstallmentBalance, InvestmentTypeDescription, InvestmentTypeID, IsHomeowner, LastUpdatedDate, LenderIndicator, LenderYield, ListingAmountFunded, ListingCategory, ListingCreationDate, ListingEndDate, ListingMonthlyPayment, ListingNumber, ListingPurpose, ListingRequestAmount, ListingStartDate, ListingStatus, ListingStatusDescription, ListingTerm, ListingTitle, LoanNumber, LoanOriginationDate, MaxPriorProsperLoan, MemberKey, MinPriorProsperLoan, MonthlyDebt, MonthsEmployed, NowDelinquentDerog, Occupation, OldestTradeOpenDate, OpenCreditLines, PartialFundingIndicator, PercentFunded, PriorProsperLoanEarliestPayOff, PriorProsperLoans, PriorProsperLoans31DPD, PriorProsperLoans61DPD, PriorProsperLoansActive, PriorProsperLoansBalanceOutstanding, PriorProsperLoansCyclesBilled, PriorProsperLoansLateCycles, PriorProsperLoansLatePaymentsOneMonthPlus, PriorProsperLoansOnTimePayments, PriorProsperLoansPrincipalBorrowed, PriorProsperLoansPrincipalOutstanding, ProsperRating, ProsperScore, PublicRecordsLast10Years, PublicRecordsLast12Months, RealEstateBalance, RealEstatePayment, RevolvingAvailablePercent, RevolvingBalance, SatisfactoryAccounts, ScoreX, ScoreXChange, StatedMonthlyIncome, TotalInquiries, TotalOpenRevolvingAccounts, TotalTradeItems, VerificationStage, WasDelinquentDerog, WholeLoanEndDate, WholeLoanStartDate'''

      keys = ['ListingNumber', 'DelinquenciesLast7Years', 'FICOScore',
              'AmountDelinquent', 'BankcardUtilization',
              'BorrowerListingDescription', 'CreditLinesLast7Years',
              'EmploymentStatusDescription', 'EstimatedReturn',
              'FirstRecordedCreditLine', 'IsHomeowner', 'ListingPurpose',
              'Occupation', 'PercentFunded', 
      ]
      if len(listdict) == 0:
         print 'No listings found...'
         return
      for listing in listdict:
         print '-' * 80
         for key in keys:
            print '%s: %s' % (key, listing[key])
         print '-' * 80
         print

def main():
   from optparse import OptionParser
   parser = OptionParser(usage='usage: %prog [options] filename',
                         version='%prog 1.0')
   parser.add_option('-x', '--xhtml',
                     action='store_true',
                     dest='xhtml_flag',
                     default=False,
                     help='create a XHTML template instead of HTML')
   parser.add_option('-u', '--username',
                     dest='username',
                     default='noel@sdsmt.net',
                     help='Prosper account username',)
   parser.add_option('-p', '--password',
                     dest='password',
                     help='Prosper account password',)
   parser.add_option('-r', '--restorefile',
                     dest='restorefile',
                     help='Restore previous data from pickle file',)

   (options, args) = parser.parse_args()

   #print parser.print_help()
   #parser.error('wrong number of arguments')

   obj = prosperclient(options.username, options.password, options.restorefile)

if __name__ == '__main__':
   main()
